package com.jinkaimen.kqb.dao;

import android.accounts.Account;
import android.content.Context;
import android.content.SharedPreferences;

import com.ht.baselibs.application.HTApplication;
import com.jinkaimen.kqb.bean.AccountBean;


public class AccountDao {

    public static final String TABLE_NAME = "account";
    public static final String MID = "mid";
    private static final String ACCOUNT_CELLPHONE = "account_cellphone";
    private static final String ACCOUNT_ID = "account_id";
    private static final String ACCOUNT_STATUS = "account_status";
    private static final String ACCOUNT_PROFILE = "account_profile";
    private static final String ACCOUNT_AUTHOR = "account_author";
    private static final String ACCOUNT_AUSTATUS= "account_austatus";
    private static final String ACCOUNT_ACCOUNTID= "account_accountID";
    private static final String ACCOUNT_ROLE= "account_role";


    private static AccountDao instance;
    private final SharedPreferences mPreferences;

    private AccountDao() {
        mPreferences = HTApplication.getContext().getSharedPreferences(TABLE_NAME, Context.MODE_PRIVATE);
    }

    public static AccountDao getInstance() {
        synchronized (Account.class) {
            if (instance == null) {
                instance = new AccountDao();
            }
        }
        return instance;
    }

    public void addAccount(AccountBean account){
        mPreferences.edit()//
                .putLong(ACCOUNT_ID, account.id)//
                .putLong(MID, account.mid)//
                .putString(ACCOUNT_CELLPHONE, account.cellphone)//
                .putString(ACCOUNT_PROFILE, account.profile)//
                .putInt(ACCOUNT_STATUS, account.status)//
                .putInt(ACCOUNT_AUTHOR, account.auStep)//
                .putInt(ACCOUNT_AUTHOR, account.auStep)//
                .putInt(ACCOUNT_AUSTATUS, account.auStatus)//
                .putLong(ACCOUNT_ACCOUNTID, account.accountID)//
                .putInt(ACCOUNT_ROLE, account.role)//
                .commit();
    }

    public void clear(){
        mPreferences.edit()//
                .putLong(ACCOUNT_ID, 0)//
                .putLong(MID, 0)//
                .putString(ACCOUNT_CELLPHONE, "")//
                .putString(ACCOUNT_PROFILE, "")//
                .putInt(ACCOUNT_STATUS, 0)//
                .putInt(ACCOUNT_AUTHOR, 0)//
                .putInt(ACCOUNT_AUSTATUS, 0)//
                .putLong(ACCOUNT_ACCOUNTID, 0)//
                .putInt(ACCOUNT_ROLE, 0)//
                .commit();
    }

    public AccountBean getAccount() {
        AccountBean account = new AccountBean();
        account.id = mPreferences.getLong(ACCOUNT_ID, 0);
        account.cellphone = mPreferences.getString(ACCOUNT_CELLPHONE, "");
        account.profile = mPreferences.getString(ACCOUNT_PROFILE, "");
        account.status = mPreferences.getInt(ACCOUNT_STATUS, 0);
        account.auStep = mPreferences.getInt(ACCOUNT_AUTHOR, 0);
        account.auStatus = mPreferences.getInt(ACCOUNT_AUSTATUS, 0);
        account.accountID = mPreferences.getLong(ACCOUNT_ACCOUNTID, 0);
        account.role = mPreferences.getInt(ACCOUNT_ROLE, 0);
        account.mid = mPreferences.getLong(MID, 0);
        return account;
    }

    public void authorStep(int step){
        mPreferences.edit()//
                .putInt(ACCOUNT_AUTHOR, step)//
                .commit();
    }

    public void accoutId(long accoutId){
        mPreferences.edit()//
                .putLong(ACCOUNT_ACCOUNTID, accoutId)//
                .commit();
    }

    public void authorStatus(int auStatus){
        mPreferences.edit()//
                .putInt(ACCOUNT_AUSTATUS, auStatus)//
                .commit();
    }

}